<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonModule for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace User;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module implements AutoloaderProviderInterface
{
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
		    // if we're in a namespace deeper than one level we need to fix the \ in the path
                    __NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__),
                ),
            ),
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
    
	public function getControllerPluginConfig() {
        return array(
            'factories' => array(
                'UserModuleSettingsPlugin' => function ($sm) {
        			$serviceLocator = $sm->getServiceLocator();
                    $moduleSettingsModel = $serviceLocator->get('User\Model\ModuleSettingsModel');
                    $controllerPlugin = new Controller\Plugin\ModuleSettingsPlugin();
                    $controllerPlugin->setModuleSettings($moduleSettingsModel->getSettings());
                    return $controllerPlugin;
                },
                'UserAuthenticationPlugin' => function ($sm) {
                    $serviceLocator = $sm->getServiceLocator();
                    $authService = $serviceLocator->get('User\AuthService');
                    $controllerPlugin = new Controller\Plugin\AuthenticationPlugin;
                    $controllerPlugin->setAuthService($authService);
                    return $controllerPlugin;
                },
            ),
        );
    }
    
	public function getViewHelperConfig() {
        return array(
            'factories' => array(
                'UserName' => function ($sm) {
                    $serviceLocator = $sm->getServiceLocator();
                    $viewHelper = new View\Helper\UserName;
                    $viewHelper->setAuthService($serviceLocator->get('User\AuthService'));
                    return $viewHelper;
                },
                'UserIdentity' => function ($sm) {
                    $serviceLocator = $sm->getServiceLocator();
                    $viewHelper = new View\Helper\UserIdentity;
                    $viewHelper->setAuthService($serviceLocator->get('User\AuthService'));
                    return $viewHelper;
                },
            ),
        );
    }
    
	public function getServiceConfig() {
        return array(
        	'invokables' => array(
                'User\Authentication\AuthStorage' => 'User\Authentication\AuthStorage',
            ),
        	'factories' => array(
                'User\Model\ModuleSettingsModel' => function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new Model\ModuleSettingsModel($dbAdapter);
                    return $table;
                },
                'User\Model\UserModel' => function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new Model\UserModel($dbAdapter);
                    return $table;
                },
                'User\AuthService' => function($sm) {
                	$userModel = $sm->get('User\Model\UserModel');
		            $dbTableAuthAdapter  = new \Zend\Authentication\Adapter\DbTable(
		            	$userModel->getAdapter(), $userModel->getTable(), 'email', 'password'
		            );
		            
		            return new \Zend\Authentication\AuthenticationService(
		            	$sm->get('User\Authentication\AuthStorage'),
		            	$dbTableAuthAdapter
                    );
		        },
			),
		);
	}
	
    public function onBootstrap(MvcEvent $e)
    {
        // You may not need to do this if you're doing it elsewhere in your
        // application
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    }
}